package com.glela.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.exception.PlatformException;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestPromotion;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.service.PromotionService;
import com.glela.platform.util.ParameterUtils;

/**
* @ClassName: PromotionController 
* @Description: 优惠券业务控制器
* @author NingChongQing
* @date 2017年11月14日 下午2:43:54
 */
@Controller
@RequestMapping(value = "/promotion")
public class PromotionController {
	private static Logger logger = LoggerFactory.getLogger(PromotionController.class);
	@Autowired
	private PromotionService promotionService;
	
	/**
	* **********************************************
	* @Title: givePromotionPackage 
	* @Description: 赠送优惠券礼包接口 
	* @param @param model
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2017年11月14日 下午2:53:11 
	* **********************************************
	 */
	@RequestMapping(value = "/givePromotionPackage", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> givePromotionPackage(@RequestBody RequestPromotion requestModel)
	{
		try {
			if (ParameterUtils.validParams(requestModel.getGiver(),requestModel.getAccepter(),requestModel.getPromotionId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return this.promotionService.givePromotionPackage(requestModel.getGiver(),requestModel.getAccepter(),requestModel.getPromotionId());
		} catch (Exception e) {
			logger.error("赠送优惠券礼包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	* **********************************************
	* @Title: preLoadPromotionPackages 
	* @Description: 预加载可领取礼包接口
	* @param @param requestModel
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2017年11月14日 下午3:44:01 
	* **********************************************
	 */
	@RequestMapping(value = "/preLoadPromotionPackages", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> preLoadPromotionPackages(@RequestBody RequestPromotion requestModel)
	{
		try {
			if (ParameterUtils.validParams(requestModel.getAccepter())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return this.promotionService.preLoadPromotionPackages(requestModel.getAccepter());
		} catch (Exception e) {
			logger.error("预加载可领取礼包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	* **********************************************
	* @Title: getPromotionPackages 
	* @Description: 领取优惠券礼包接口
	* @param @param requestModel 需要告诉接口，当前是谁在领取礼包
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2017年11月15日 上午8:37:19 
	* **********************************************
	 */
	@RequestMapping(value = "/getPromotionPackages", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getPromotionPackages(@RequestBody RequestPromotion requestModel)
	{
		try {
			if (ParameterUtils.validParams(requestModel.getAccepter())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return this.promotionService.addPromotionPackages(requestModel.getAccepter());
		} catch (PlatformException e) {
			logger.error("领取优惠券礼包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C43004), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("领取优惠券礼包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
}
